import {ajax} from 'nc-lightapp-front';
import requestApi from "../requestApi";

export default function (props) {

    props.createUIDom(
        {
            pagecode: "201102CJK",//页面编码
            appcode: "201102BCLF"//小应用编码
        },
        (dataui) => {
            let metas = dataui.template;
            let tempArr = [];
            let searchitem = metas[window.cjkAreaCode.searchArea];
            let headmeta = metas[window.cjkAreaCode.billsArea];
            let bodymeta = metas[window.cjkAreaCode.bodysArea];            
            let bxdata = window.cjkdata;
            let PsndocTreeFilterPath ='nccloud.web.erm.action.ref.sqlbuilder.PsndocTreeGridRef';//人员
            searchitem.items.forEach((item) => {

                if (item.attrcode == 'jkbxr') {
                    item.queryCondition = ()=> {
                        return {tradetype:bxdata.tradetype,pk_org:bxdata.dwbm,org_id:bxdata.dwbm,GridRefActionExt:PsndocTreeFilterPath,DataPowerOperationCode:'fi',/**使用权组**/isDataPowerEnable:'Y'};                
                    }
                }

                 if(item.itemtype == 'refer'){
                     item.isShowUnit=true;
                 }
                 if(item.attrcode && item.attrcode=="pk_org"){
                     item.initialvalue = {value:table,display:table}
                 }
                 tempArr.push(item);
             });

             //还款原币金额不走模板配置,不可编辑(产品设计如此)
             bodymeta.items.forEach(item => {
                if (item.attrcode ==='hkybje') {
                    item.disabled = true       
                }
             })

            let meta = {
                ...metas,
                searchArea: {
                    moduletype: 'search',
                    items : tempArr
                },
                head:{
                    moduletype: 'table',
                    items: headmeta.items
                },
                body:{
                    moduletype: 'table',
                    items: bodymeta.items
                }
            }
            props.meta.setMeta(meta);
            let data={
                querycondition:props.search.getAllSearchData("20110ETLB"),
                pagecode: window.pageCode.searchPage,
                pageInfo:{
                    "pageIndex":-1,
                    "pageSize":10,
                    "totalPage":"0"
                },
                queryAreaCode:window.cjkAreaCode.searchArea,  //查询区编码
                oid: meta[window.cjkAreaCode.searchArea].oid,  //pub_area 中PK_AREA
                querytype:'tree'
            };
            let bxcontrastGrid = window.bxcontrastGrid;
            let senddata = {'bxcontrastGrid':bxcontrastGrid ,bxdata,data,"pagecode":"20110ETLB_cjk","tepmid":props.meta.getMeta().pageid};
            requestApi.query({
                data:senddata,
                success: (data) => {
                    if(data){
                        this.setTransferBtnDisabled(false);
                        //处理已勾选的情况还款原币金额汇总问题
                        if (data.length > 0) {
                            data.forEach((dataitem) => {
                                let hkybje_sethead=0.00;
                                let cjkybje_head = 0.00
                                let scale = '4'
                                if (dataitem.body.jk_busitem) {
                                    for (let num = 0; num < dataitem.body.jk_busitem.rows.length; num++) {
                                        if (
                                            dataitem.body.jk_busitem.rows[num].values.hkybje &&
                                            dataitem.body.jk_busitem.rows[num].values.hkybje.value != 'NaN'
                                        ) {
                                            hkybje_sethead += parseFloat(dataitem.body.jk_busitem.rows[num].values.hkybje.value);
                                            scale = dataitem.body.jk_busitem.rows[num].values.hkybje.scale;
                                        }
                                        if (
                                            dataitem.body.jk_busitem.rows[num].values.cjkybje &&
                                            dataitem.body.jk_busitem.rows[num].values.cjkybje.value != 'NaN'
                                        ) {
                                            cjkybje_head += parseFloat(dataitem.body.jk_busitem.rows[num].values.cjkybje.value);
                                        }
                                    }
                                }
                                dataitem.head.head.rows[0].values.hkybje =  { display:String(hkybje_sethead.toFixed(scale)),scale, value:String(hkybje_sethead.toFixed(scale)) };
                                dataitem.head.head.rows[0].values.cjkybje =  { display:String(cjkybje_head.toFixed(scale)),scale, value:String(cjkybje_head.toFixed(scale)) };
                                // dataitem.head.head.rows[0].values.cjkybje =  { display: cjkybje_head, scale, value: cjkybje_head}
                            });
                        }

                        data && this.props.transferTable.setTransferTableValue(window.cjkAreaCode.billsArea, window.cjkAreaCode.bodysArea, data, 'pk_jkbx', 'pk_busitem');
                        props.setDefaultSelectedJk(data, this.props);
                        
                    }else{
                        this.setTransferBtnDisabled(true);
                        this.props.transferTable.setTransferTableValue(window.cjkAreaCode.billsArea, window.cjkAreaCode.bodysArea, [], 'pk_jkbx', 'pk_busitem');
                    }

                }
            })
    });

}
